﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.ComponentModel;
namespace Deloitte
{
   public class Reflection
    {
       public static void AddPropertiesToComboBox(Type type, System.Windows.Forms.ToolStripComboBox comboBox, AutoCompleteMode autoCompleteMode)
        {
            if (type != null)
            {
                AutoCompleteStringCollection stringColletion = new AutoCompleteStringCollection();
                List<PropertyInfo> props = new List<PropertyInfo>();
                List<PropertyInfo> stringProps = Reflection.GetStringProperties(type);
                foreach (PropertyInfo prop in stringProps) { props.Add(prop); }
                List<PropertyInfo> boolProps = Reflection.GetBooleanProperties(type);
                foreach (PropertyInfo prop in boolProps) { props.Add(prop); }
                foreach (PropertyInfo prop in props)
                {
                    comboBox.Items.Add(prop.Name);
                    stringColletion.Add(prop.Name);
                }
                comboBox.AutoCompleteCustomSource = stringColletion;
                try { comboBox.AutoCompleteMode = autoCompleteMode; }
                catch { }
            }
        }
       private static List<PropertyInfo> GetProperties(Type type)
       {
           List<PropertyInfo> props = new List<PropertyInfo>();
           foreach (PropertyInfo prop in type.GetProperties())
           {
               props.Add(prop);
           }
           return props;
       }      

       private static List<PropertyInfo> GetBooleanProperties(Type type)
       {
           List<PropertyInfo> props = new List<PropertyInfo>();
           foreach (PropertyInfo prop in type.GetProperties())
           {
               if (prop.PropertyType.FullName == typeof(bool).FullName)
               {
                   props.Add(prop);
               }
           }
           return props;
       }

       private static List<PropertyInfo> GetStringProperties(Type type)       
       {
           List<PropertyInfo> props = new List<PropertyInfo>();           
           foreach (PropertyInfo prop in type.GetProperties())
           {
               if (prop.PropertyType.FullName == typeof(string).FullName)
               {
                   props.Add(prop);                   
               }
           }
           return props;
       }
      
       /// <summary>
       /// Binds the properties of an object to a DataGridView
       /// </summary>
       /// <param name="obj"></param>
       /// <param name="dgv"></param>
       public static void BindPropertiesToDataGridView(Type type, DataGridView dgv)
       {
           BindStringPropertiesToDataGridView(type, dgv);
           BindBooleanPropertiesToDataGridView(type, dgv);
       }

       private static void BindBooleanPropertiesToDataGridView(Type type, DataGridView dgv)
       {
           DataGridViewCheckBoxColumn checkColumn = null;
           List<PropertyInfo> props = GetBooleanProperties(type);
           foreach (PropertyInfo prop in props)
           {
               checkColumn = new DataGridViewCheckBoxColumn();
               checkColumn.DataPropertyName = prop.Name;
               checkColumn.HeaderText = prop.Name;
               checkColumn.Name = prop.Name + checkColumn.GetType().Name;
               dgv.Columns.Add(checkColumn);
           }
       }

       private static void BindStringPropertiesToDataGridView(Type type, DataGridView dgv)
       {
           DataGridViewTextBoxColumn textColumn = null;
           List<PropertyInfo> props = GetStringProperties(type);
           foreach (PropertyInfo prop in props)
           {
               textColumn = new DataGridViewTextBoxColumn();
               textColumn.DataPropertyName = prop.Name;
               textColumn.HeaderText = prop.Name;
               textColumn.Name = prop.Name + textColumn.GetType().Name;
               dgv.Columns.Add(textColumn);
           }
       }

       public static void AddPropertiesToDataGridViewComboBox(Type type, System.Windows.Forms.DataGridViewComboBoxColumn comboBox)
       {
           if (type != null)
           {               
               List<PropertyInfo> props = Reflection.GetStringProperties(type);
               foreach(PropertyInfo prop in props)
               {
                   comboBox.Items.Add(prop.Name);    
               }                                                                                                  
           }           
       }
     
    }
}